from typing import List
from pydantic import BaseModel, Field, root_validator
from ..common.sec_level import PostCode, HeadTailFulfillment
from ..common.sec_level import Customer

"""
订单相关
"""


class OrderBaseForUserCenter(BaseModel):
    """
    用户中心订单
    """
    id: int = Field(None, description='订单ID')
    code: str = Field(None, description='订单号')
    fulfillment_list: List[HeadTailFulfillment] = Field(None, description='头尾程列表')
    from_postal_code: PostCode = Field(None, description='起始邮编')
    to_postal_code: PostCode = Field(None, description='目标邮编')
    parcel_number: int = Field(None, description='包裹数量')
    order_time: str = Field(None, description='下单时间')
    payment_create_time: str = Field(None, description='支付单创建时间')
    state: str = Field(None, description='状态')
    total_actual_price: float = Field(None, description='总实价')
    pay_link: str = Field(None, description='支付链接')
    expired_time: int = Field(30, description='截止时间')
    remaining_time: int = Field(None, description='剩余时间')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['code'] = mdl.code
            values['fulfillment_list'] = [HeadTailFulfillment(__from_odoo=record, __env=env) for record in mdl.rp_ship_order_fulfillment_line_ids]
            values['from_postal_code'] = PostCode(__from_odoo=mdl.send_postal_code_id, __env=env)
            values['to_postal_code'] = PostCode(__from_odoo=mdl.receive_postal_code_id, __env=env)
            values['parcel_number'] = len(mdl.rp_parcel_ids.ids)
            values['order_time'] = mdl.create_date.strftime('%Y-%m-%d %H:%M:%S')
            values['total_actual_price'] = mdl.total_actual_price
            values['pay_link'] = mdl.pay_link
            values['state'] = mdl.state
            values['expired_time'] = env['ir.config_parameter'].get_param('ship_order.expired_time')
            values['payment_create_time'] = env['rp.payment'].search([('advance_document_id.field_name_id.model', '=', 'rp.ship.order'), ('advance_document_id.field_record_id', '=', mdl.id)]).time_create.strftime('%Y-%m-%d %H:%M:%S') \
                if env['rp.payment'].search([('advance_document_id.field_name_id.model', '=', 'rp.ship.order'), ('advance_document_id.field_record_id', '=', mdl.id)]) else False
            values['remaining_time'] = env['rp.payment'].search([('advance_document_id.field_name_id.model', '=', 'rp.ship.order'), ('advance_document_id.field_record_id', '=', mdl.id)]).remaining_second if env['rp.payment'].search(
                [('advance_document_id.field_name_id.model', '=', 'rp.ship.order'), ('advance_document_id.field_record_id', '=', mdl.id)]).remaining_second else False
        return values


class OrderBaseForUserCenterVO(BaseModel):
    """
    加入总条目
    """
    order_list: List[OrderBaseForUserCenter] = Field(None, description='订单列表')
    amount: int = Field(None, description='订单数')


class ShipmentInvoice(BaseModel):
    """
    订单发票
    """
    code: str = Field(None, description='发票Code')
    type: str = Field(None, description='类型')
    order_number: str = Field(None, description='订单ID')
    issue_date: str = Field(None, description='发布日期')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values['__from_odoo']
        env = values['__env']
        if mdl and env:
            values['code'] = mdl.code
            values['type'] = 'Invoice'
            values['order_number'] = env['rp.ship.order'].search([('id', '=', mdl.advanced_document_ids[0].field_record_id)]).code
            values['issue_date'] = mdl.issue_datetime.strftime('%Y-%m-%d %H:%M:%S')
        return values


"""
用户信息相关
"""


class Address(BaseModel):
    """
    地址
    """
    id: int = Field(None, description='地址ID')
    rel_type: str = Field(None, description='关联类型')
    default_address: bool = Field(None, description='默认地址')
    address_type: str = Field(None, description='地址类型')
    contact_company_name: str = Field(None, description='公司名称')
    last_name: str = Field(None, description='名')
    first_name: str = Field(None, description='姓')
    area_code: str = Field(None, description='区号')
    mobile: str = Field(None, description='手机')
    street: str = Field(None, description='街道1')
    street2: str = Field(None, description='街道2')
    email: str = Field(None, description='邮箱')
    postal_code: PostCode = Field(None, description='邮编')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['rel_type'] = mdl.rel_type
            values['default_address'] = mdl.default_address
            values['address_type'] = mdl.address_type
            values['contact_company_name'] = mdl.contact_company_name
            values['last_name'] = mdl.last_name
            values['first_name'] = mdl.first_name
            values['area_code'] = mdl.area_code
            values['mobile'] = mdl.mobile
            values['street'] = mdl.street
            values['street2'] = mdl.street2
            values['email'] = mdl.email
            values['postal_code'] = PostCode(__from_odoo=mdl.postal_code_id, __env=env)
        return values


class UserInfo(BaseModel):
    """
    用户信息
    """
    id: int = Field(None, description='用户ID')
    profile_url: str = Field(None, description='头像链接')
    last_name: str = Field(None, description='名')
    first_name: str = Field(None, description='姓')
    area_code: str = Field(None, description='区号')
    phone_number: str = Field(None, description='手机号')
    email: str = Field(None, description='注册邮箱')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['profile_url'] = (env['ir.attachment'].search([('res_id', '=', mdl.id), ('res_field', '=', 'profile_image')], limit=1)).url
            values['last_name'] = mdl.last_name
            values['first_name'] = mdl.first_name
            values['area_code'] = mdl.area_code
            values['phone_number'] = mdl.phone_number
            values['email'] = mdl.email
        return values


class UserBalance(BaseModel):
    """
    用户资金
    """
    rp_customer: Customer = Field(None, description='客户')
    total_price: float = Field(None, description='总余额')
    actual_price: float = Field(None, description='实付金额')
    present_price: float = Field(None, description='赠送金额')
    actual_radio: float = Field(None, description='实付比率')
    present_radio: float = Field(None, description='赠送比率')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['rp_customer'] = Customer(__from_odoo=mdl.rp_customer_id, __env=env)
            values['total_price'] = mdl.total_balance
            values['actual_price'] = mdl.actual_price
            values['present_price'] = mdl.present_price
            values['actual_radio'] = mdl.actual_radio
            values['present_radio'] = mdl.present_radio
        return values


"""
邀请推广相关 
"""


class CouponBase(BaseModel):
    """
    优惠券基本信息
    """
    id: int = Field(None, description='优惠券ID')
    name: str = Field(None, description='优惠券名称')
    par_value: float = Field(None, description='面值')
    discount_value: float = Field(None, description='折扣值')
    coupon_threshold: float = Field(None, description='门槛值')
    coupon_type: str = Field(None, description='优惠券类型')
    coupon_status: str = Field(None, description='优惠券状态')
    expired_time: str = Field(None, description='截止时间')
    remark: str = Field(None, description='备注')

    @root_validator(pre=True)
    def create(cls, values):
        mdl = values.get('__from_odoo')
        env = values.get('__env')
        if mdl and env:
            values['id'] = mdl.id
            values['name'] = mdl.name
            values['par_value'] = mdl.par_value
            values['discount_value'] = mdl.discount_value
            values['coupon_threshold'] = mdl.coupon_threshold
            values['coupon_type'] = mdl.coupon_type
            values['coupon_status'] = mdl.rp_coupon_status
            values['expired_time'] = mdl.customer_coupon_expired_date.strftime('%Y-%m-%d %H:%M:%S') if mdl.customer_coupon_expired_date else None
            values['remark'] = mdl.remark
        return values
